package client;

import java.io.*;

import util.*;

public class ClientProtocol {
	BufferedReader in;
	PrintWriter out;
	int playerID;
	public ClientProtocol(int tplayerID, PrintWriter tout, BufferedReader tin) {
		this.playerID = tplayerID;
		this.out=tout;
		this.in=tin;
	}
	
	public void sendActionSet(ActionSet as) {
		out.println(as.playerID+" "+as.turnID);
		out.println(as.moveX+" "+as.moveY);
		out.println(as.castID+((as.castID==-1)?"":(" "+as.castTargetX+" "+as.castTargetY)));
	}
	public ActionSet readActionSet(){
		try {
			ActionSet as = new ActionSet();
			String[] tokens = readLineBlocking().split(" ");
			as.playerID = Integer.parseInt(tokens[0]);
			as.turnID = Integer.parseInt(tokens[1]);
			tokens = readLineBlocking().split(" ");
			as.moveX = Double.parseDouble(tokens[0]);
			as.moveY = Double.parseDouble(tokens[1]);
			tokens = readLineBlocking().split(" ");
			as.castID = Integer.parseInt(tokens[0]);
			if(as.castID!=-1) {
				as.castTargetX = Double.parseDouble(tokens[1]);
				as.castTargetY = Double.parseDouble(tokens[2]);
			}
			
			return as;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	String readLineBlocking() throws IOException, InterruptedException {
		while (!in.ready()) {
			Thread.sleep(10);
		}
		return in.readLine();
	}
}
